package com.zjeav.lingjiao.base.adapter;

import android.content.Context;
import android.view.View;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;

import com.zjeav.lingjiao.R;
import com.zjeav.lingjiao.base.baseBean.ItemResource;
import com.zjeav.lingjiao.base.baseBean.LocalMetadata;
import com.zjeav.lingjiao.base.config.BaseConfig;
import com.zjeav.lingjiao.base.tools.FileUtils;
import com.zjeav.lingjiao.base.tools.LocalUtils;
import com.zjeav.lingjiao.base.tools.TimeTools;
import com.zjeav.lingjiao.base.utils.UIUtils;
import com.zjeav.lingjiao.ui.music.MusicActivity;

import org.xutils.DbManager;

import java.util.ArrayList;

public class MetaAdapter extends BaseQuickAdapter<ItemResource, BaseViewHolder> {
    private Context context;
    private boolean isPayed;
    private int bookid;
    ArrayList<LocalMetadata> metadataArrayList;
    DbManager db;
    MetaAdapter.ClickListener listener;

    public MetaAdapter(Context context, DbManager db, MetaAdapter.ClickListener listener, int bookid) {
        super(R.layout.mate_resource_item);
        this.context = context;
        this.db = db;
        this.bookid = bookid;
        this.listener = listener;
    }

    public void isPayed(boolean isPayed) {
        this.isPayed = isPayed;
    }

    @Override
    protected void convert(final BaseViewHolder helper, final ItemResource item) {
        helper.setText(R.id.mate_content_txt, item.getTitle());
        helper.setText(R.id.mate_dur_txt, TimeTools.getDuration(item.getMetadata().getDuration()));
        if ("音频".equals(item.getType())) {
            helper.setImageResource(R.id.mate_type_img, R.mipmap.data_audio);
        } else {
            helper.setImageResource(R.id.mate_type_img, R.mipmap.data_video);
        }
//        helper.setOnItemClickListener(R.id.mate_down_img, new OnItemChildClickListener());
//        helper.addOnClickListener(R.id.mate_down_img);\
        int type = 0;//要下载 1：播放 2：锁住

        metadataArrayList = LocalUtils.getList(db, bookid);

        boolean isHas = false;
        if (metadataArrayList != null) {
            for (int i = 0; i < metadataArrayList.size(); i++) {
                if (item.getId() == metadataArrayList.get(i).getId()) {
                    isHas = true;
                }
            }
        }
        final int position = helper.getAdapterPosition();
        if (isPayed) {
            if (!isHas) {
                type = 0;
                helper.setImageResource(R.id.mate_down_img, R.mipmap.data_download);
//                helper.addOnClickListener(R.id.mate_down_img);
                helper.getView(R.id.mate_down_img).setVisibility(View.VISIBLE);
                helper.getView(R.id.mate_down_txt).setVisibility(View.GONE);
                helper.getView(R.id.music_ly).setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        if (UIUtils.isFastClick()){
                            listener.ItemClick(item, 0, position,helper.getView(R.id.mate_down_txt));
                        }
                    }
                });
            } else {
                type = 1;
                helper.setText(R.id.mate_down_txt, "已下载");
                helper.getView(R.id.mate_down_img).setVisibility(View.GONE);
                helper.getView(R.id.mate_down_txt).setVisibility(View.VISIBLE);
                helper.addOnClickListener(R.id.music_ly);
                helper.getView(R.id.music_ly).setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        if (UIUtils.isFastClick()){
                            listener.ItemClick(item, 1, position,helper.getView(R.id.mate_down_txt));
                        }

                    }
                });

            }
        } else {
            if (position == 0 || position == 1) {
                if (!isHas) {
                    helper.setImageResource(R.id.mate_down_img, R.mipmap.data_download);
                    helper.addOnClickListener(R.id.mate_down_img);
                    helper.getView(R.id.mate_down_img).setVisibility(View.VISIBLE);
                    helper.getView(R.id.mate_down_txt).setVisibility(View.GONE);
                    helper.getView(R.id.music_ly).setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
                            if (UIUtils.isFastClick()){
                                listener.ItemClick(item, 0, position,helper.getView(R.id.mate_down_txt));
                            }

                        }
                    });
                } else {
                    helper.setText(R.id.mate_down_txt, "已下载");
                    helper.getView(R.id.mate_down_img).setVisibility(View.GONE);
                    helper.getView(R.id.mate_down_txt).setVisibility(View.VISIBLE);
                    helper.addOnClickListener(R.id.music_ly);
                    helper.getView(R.id.music_ly).setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
                            if (UIUtils.isFastClick()){
                                listener.ItemClick(item, 1, position,helper.getView(R.id.mate_down_txt));
                            }

                        }
                    });

                }
            } else {
                helper.setImageResource(R.id.mate_down_img, R.mipmap.file_locked);
//                helper.addOnClickListener(R.id.mate_down_img);
                helper.getView(R.id.mate_down_img).setVisibility(View.VISIBLE);
                helper.getView(R.id.mate_down_txt).setVisibility(View.GONE);
                helper.addOnClickListener(R.id.music_ly);
                helper.getView(R.id.music_ly).setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        if (UIUtils.isFastClick()){
                            listener.ItemClick(item, 2, position,helper.getView(R.id.mate_down_txt));
                        }

                    }
                });
            }
        }


    }


    public interface ClickListener {
        void ItemClick(ItemResource itemResource, int type, int position,View view);
    }
}
